iT邦幫忙

第 12 屆 iThome 鐵人賽

DAY 19
0
Mobile Development

Flutter App 開發實戰系列 第 19

Flutter Dialog 範例 [DAY 19]

  • 分享至 

  • xImage
  •  

今天來看看 alertDialog , simpleDialog 該如何來使用,介紹些常用的範例


AlertDialog

標題

    showDialog(
      context: context,
      builder: (BuildContext context){
        return AlertDialog(
          title: Text("title"),
          content: Text("content info "),
        );
      }
    );

https://ithelp.ithome.com.tw/upload/images/20200919/20130127rbFmB2JK6o.png
按鈕

 showDialog(
        context: context,
        builder: (BuildContext context){
          return AlertDialog(
            title: Text("title"),
            actions: <Widget>[
              FlatButton(
                child: Text("OK"),
                onPressed: (){
                  Navigator.of(context).pop();
                },
              ),
              FlatButton(
                child: Text("CANCEL" , style: TextStyle(color: Colors.redAccent),),
                onPressed: (){
                  Navigator.of(context).pop();
                },
              )
            ],

          );
        }
    );

https://ithelp.ithome.com.tw/upload/images/20200919/201301278LxkWpkIob.png

陰影

   showDialog(
        context: context,
        builder: (BuildContext context){
          return AlertDialog(
          //elevation 可以改變陰影深度
            elevation: 0,
            title: Text("elevation"),
          );
        }
    );

https://ithelp.ithome.com.tw/upload/images/20200919/20130127u9AYihMDlQ.png

圓角

    showDialog(
        context: context,
        builder: (BuildContext context){
          return AlertDialog(
          //shape 可以改變形狀
            shape: RoundedRectangleBorder(
                borderRadius: BorderRadius.all(Radius.circular(24.0))),
            title: Text("Shape"),
            content: Text("use RoundedRectangleBorder"),
          );
        }
    );

https://ithelp.ithome.com.tw/upload/images/20200919/20130127niDtmgqaf2.png


SimpleDialog

選項


  Future<void> showSimpleDialogShape(BuildContext context) async {
                       
    Country country =  await showDialog<Country>(//接收返回的狀態
        context: context,
        builder: (BuildContext context){
          return SimpleDialog(
            title: Text("Simple Demo"),
            //在 children 中可以加入選項
            children: <Widget>[
              SimpleDialogOption(
                onPressed: (){
                //pop 時傳送 result 的值
                  Navigator.pop(context,Country.Australia) ;
                },
                child: Text(Country.Australia.toString()),),
              SimpleDialogOption(
                onPressed: (){
                  Navigator.pop(context,Country.Brazil) ;
                },
                child: Text(Country.Brazil.toString()),),
              SimpleDialogOption(
                onPressed: (){
                  Navigator.pop(context,Country.Canada) ;
                },
                child: Text(Country.Canada.toString()),),
            ],
          );
        }
    ) ;
    switch (country){
      case Country.Australia:
        print(Country.Australia.toString());
        break;
      case Country.Brazil:
      //do something
      case Country.Canada:
      //do something
    }

  }

https://ithelp.ithome.com.tw/upload/images/20200919/20130127KnHuP0D0e9.png


上一篇
Flutter Dialog 介紹 [DAY 18]
下一篇
Flutter Dialog Full Screen [DAY 20]
系列文
Flutter App 開發實戰30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言